//
//  KYServer.m
//  KYAnalytics
//
//  Created by 罗新 on 2017/10/16.
//  Copyright © 2017年 kuaiyan. All rights reserved.
//

#import "KYServer.h"
#import "KYHttpClient.h"
#import "KYAES128.h"
#import "KYHeader.h"

#define AFRequestMethodPost     @"POST"
#define AFRequestMethodGet      @"GET"
#define GDIPA_KEY               @"8d0c27eee648108f745cb413547aa86a"

@implementation KYServer

/**
 上传用户硬件信息到服务器
 */
+ (void)postUserDeviceInfoWithAppkey:(NSString *)appkey success:(void (^)(NSDictionary *))successBlock failure:(void (^)(NSString *))failureBlock{
    [self saveToSandboxWithKey:@"kyAppkey" value:appkey];
    NSMutableDictionary *parameDict = [NSMutableDictionary dictionary];
    NSData *data = [NSJSONSerialization dataWithJSONObject:[KYHttpClient uaDic] options:NSJSONWritingPrettyPrinted error:nil];
    NSString *dataStr = [[NSString alloc] initWithData:[data base64EncodedDataWithOptions:0] encoding:NSUTF8StringEncoding];
    NSString *encryeptStr = [KYAES128 AES128Encrype:dataStr];;
    parameDict[@"v"] = encryeptStr;
    [KYHttpClient sendRequest:[NSString stringWithFormat:@"%@/%@", kAppServerForHttps, vPostInterface] requestMethod:AFRequestMethodGet parameters:parameDict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if(successBlock)successBlock(dic);
    } failure:^(NSUInteger statusCode, NSString *error) {
        if(failureBlock)failureBlock(error);
    }];
}

/**
 请求地理信息
 */
+ (void) requestAddressDataSuccess:(void (^)(NSDictionary *dataDict))successBlock
                           failure:(void(^)(NSString *failureStr))failureBlock{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:GDIPA_KEY forKey:@"key"];
    [dict setValue:@"json" forKey:@"output"];
    [KYHttpClient sendRequest:AddressInterface requestMethod:AFRequestMethodPost parameters:dict success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if ([[dic valueForKey:@"status"] isEqualToString:@"1"]) {
            NSString *recStr = [NSString stringWithFormat:@"%@", [dic valueForKey:@"rectangle"]];
            if (!IsStrEmpty(recStr) && [recStr containsString:@";"]) {
                NSArray *jwData = [[dic valueForKey:@"rectangle"] componentsSeparatedByString:@";"];
                NSMutableArray *jwArr = [NSMutableArray array];
                for (int i = 0; i < jwData.count; i++) {
                    NSArray *arr = [jwData[i] componentsSeparatedByString:@","];
                    for (int j = 0; j < arr.count; j++) {
                        [jwArr addObject:arr[j]];
                    }
                }
                [self saveToSandboxWithKey:@"kyLatitude" value:[NSString stringWithFormat:@"%lf", ([jwArr[1] doubleValue] + [jwArr[3] doubleValue])/2.]];
                [self saveToSandboxWithKey:@"kyLongitude" value:[NSString stringWithFormat:@"%lf", ([jwArr[0] doubleValue] + [jwArr[2] doubleValue])/2.]];
            }
            if (successBlock) successBlock(dic);
        }
    } failure:^(NSUInteger statusCode, NSString *error) {
        if (failureBlock) failureBlock(error);
    }];
}

/**
 软件安装时间
 */
+ (void)refreshAppInstallTime{
    if (IsStrEmpty([[NSUserDefaults standardUserDefaults] objectForKey:@"kyAppInstallTime"])) {
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval a=[dat timeIntervalSince1970];
        NSString *timeString = [NSString stringWithFormat:@"%ld", (long)a];
        [self saveToSandboxWithKey:@"kyAppInstallTime" value:timeString];
    }
}

/**
 数据保存到沙盒
 */
+ (void)saveToSandboxWithKey:(NSString *)key value:(NSString *)value{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:value forKey:key];
    [userdefault synchronize];
}
     
@end
